package monsmon.control;

import monsmon.game.*;

public class EControl{

	/*
	 *	Puts an entity at a location at LevelMap "to", coord (x,y)
	 *	nulls old location
	 *
	 *	if x,y is not in the lvl, it returns
	 *
	 */
	public static void ent_move(LevelMap to, Entity e, int x, int y,
			boolean step){
		if(x < 0 || x >= to.get_x_len() || y < 0 || to.get_y_len() <= y){
				return;
		}
		LevelMap from = e.get_lvl();
		int xfm = e.get_x(), yfm = e.get_y();
		from.get_tile(xfm, yfm).set_ent(null);
		to.get_tile(x, y).set_ent(e);
		e.set_lvl(x, y, to);
		MonsGame gm = to.get_game();
		gm.set_current_lvl(to.get_name());
		if(step){
			to.get_tile(x, y).walk_on(e);
		}
	}

	/*
	 * see above
	 */
	public static void ent_move(Entity e, int x, int y){
		ent_move(e.get_lvl(), e, x, y, true);
	}

	public static void ent_move(Entity e, Direction d){
		int x = e.get_x(), y = e.get_y();
		switch(d){
			case NORTH: y--;
				    break;
			case SOUTH: y++;
				    break;
			case EAST:  x++;
				    break;
			case WEST:  x--;
				    break;
			default:
				    break;
		}
		ent_move(e.get_lvl(), e, x, y, true);
	}

	public static boolean ent_can_move(Entity e, Direction d){
		int x = e.get_x(), y = e.get_y();
		switch(d){
			case NORTH: y--;
				    break;
			case SOUTH: y++;
				    break;
			case EAST:  x++;
				    break;
			case WEST:  x--;
				    break;
			default:
				    break;
		}
		Tile t = e.get_lvl().get_map()[x][y];
		boolean can = t.can_walk();
		can = can && t.get_ent() == null;
		return can;
	}

	public static void ent_move_if_can(Entity e, Direction d){
		if(ent_can_move(e, d)){
			ent_move(e, d);
		}
	}



}
